
package com.itheima.Test01;

import com.itheima.bean.Student;
import com.itheima.bean.Studentdb3;
import com.itheima.dao.StudentDao;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class StudentTest {
    InputStream is;
    StudentDao studentMapper =null;
    SqlSession sqlSession =null;

    {
        try {
            is = Resources.getResourceAsStream("StudentMapper.xml");
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            sqlSession = sqlSessionFactory.openSession(true);
            studentMapper = sqlSession.getMapper(StudentDao.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @Test
    public void selectAllStudent() throws IOException {
        List<Student> students = studentMapper.selectAllStudent();
        for (Student student:students) {
            System.out.println(student);
        }
        sqlSession.close();
        is.close();
    }
    @Test
    public void selectByIdStudent() throws IOException {
        Studentdb3 student = studentMapper.selectByIdStudent(1);
        System.out.println(student);
        sqlSession.close();
        is.close();
    }
 @Test
    public void insertStudent() throws IOException {
     Studentdb3 studentdb3 = new Studentdb3(5,"wangwu",18);
        int insertCount = studentMapper.insertStudent(studentdb3);
        if(insertCount>0){
            System.out.println("tianjiachenggong");
        }else {
            System.out.println("tianjiashibai");
        }
        sqlSession.close();
        is.close();
    }

    @Test
    public void deleteStudent() throws IOException {
        int deleteStudent = studentMapper.deleteStudent(5);
        sqlSession.close();
        is.close();
    }
    @Test
    public void updateStudent() throws IOException {
        Studentdb3 studentdb3 = new Studentdb3(2,"lisi",11);
        int i = studentMapper.updateStudent(studentdb3);
        sqlSession.close();
        is.close();
    }
}

